package com.aaa.marketresearch.android.data;

import android.app.Activity;
import android.content.SharedPreferences;
import com.aaa.marketresearch.android.data.domain.BillContent;
import com.aaa.marketresearch.android.data.domain.FamilyMember;
import com.aaa.marketresearch.android.data.domain.Market;
import com.aaa.marketresearch.android.data.domain.Offer;
import com.aaa.marketresearch.android.util.SimpleLog;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Nail
 */
public class UserInfo {

    private static final String USER_DATA = "MARKET_RESEARCH_USER_DATA";

    private static final UserInfo instance = new UserInfo();

    private Integer id;

    private String account;

    private String password;

    private String server;

    private String role;

    private FamilyMember currentFamilyMember;

    private Market currentMarket;

    private BillContent currentBillContent;

    private List<BillContent> bill = new ArrayList<>();

    private List<FamilyMember> familyMembers = null;

    public static UserInfo getInstance() {
        return instance;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
        ServerInfo.getInstance().setServer(server);
    }

    public boolean isAdmin() {
        if (this.role != null) {
            boolean equal = this.role.equalsIgnoreCase("ADMIN");
            return equal;
        } else {
            return false;
        }
    }

    public boolean isUser() {
        if (this.role != null) {
            boolean equal = this.role.equalsIgnoreCase("USER");
            return equal;
        } else {
            return false;
        }
    }

    public String getRole() {
        return role;
    }

    public boolean isAnonymous() {
        if (this.role != null) {
            boolean equal = this.role.equalsIgnoreCase("ANONYMOUS");
            return equal;
        } else {
            return false;
        }
    }

    public boolean isAuthorized() {
        if (this.role != null) {
            boolean equal = this.role.equalsIgnoreCase("NONE");
            return !equal;
        } else {
            return false;
        }
    }

    public Integer getId() {
        return id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public FamilyMember getCurrentFamilyMember() {
        return currentFamilyMember;
    }

    public void setCurrentFamilyMember(FamilyMember currentFamilyMember) {
        this.currentFamilyMember = currentFamilyMember;
    }

    public Market getCurrentMarket() {
        return currentMarket;
    }

    public void setCurrentMarket(Market currentMarket) {
        this.currentMarket = currentMarket;
    }

    public BillContent getCurrentBillContent() {
        if (currentBillContent == null) {
            currentBillContent = new BillContent();
        }
        return currentBillContent;
    }

    public void setCurrentBillContent(BillContent currentBillContent) {
        this.currentBillContent = currentBillContent;
    }

    public List<BillContent> getBill() {
        return bill;
    }

    public List<FamilyMember> getFamilyMembers() {
        return familyMembers;
    }

    public void setFamilyMembers(List<FamilyMember> familyMembers) {
        this.familyMembers = familyMembers;
    }

    public void setJSONData(String json) {
        if (json == null) {
            this.role = "NONE";
        } else {
            JSONParser parser = new JSONParser();
            JSONObject jUser = null;
            try {
                jUser = (JSONObject) parser.parse(json);
            } catch (ParseException e) {
                SimpleLog.error(e.toString());
            }
            Object obj = null;

            obj = jUser.get("role");
            if (obj != null) {
                this.role = (String) obj;
            }
            if (!this.role.equalsIgnoreCase("NONE")) {
                obj = jUser.get("id");
                if (obj != null && (obj instanceof Long)) {
                    this.id = ((Long) obj).intValue();
                }
            }
        }
    }

    private UserInfo() {
    }

    public void loadData(Activity activity) {
        SharedPreferences settings = activity.getSharedPreferences(USER_DATA, 0);
        this.account = settings.getString("account", "");
        this.password = settings.getString("password", "");
        this.server = settings.getString("server", ServerInfo.ServerCode.AMAZON.name());
    }

    public void saveData(Activity activity) {
        SharedPreferences settings = activity.getSharedPreferences(USER_DATA, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("account", this.account);
        editor.putString("password", this.password);
        editor.putString("server", this.server);

        editor.commit();
    }

    public JSONObject getJSONBill() {
        JSONObject jBill = new JSONObject();
        JSONArray content = new JSONArray();
        for (BillContent billContent : bill) {
            content.add(billContent.getJSON());
        }
        jBill.put("content", content);
        return jBill;
    }

    public void clear() {
        this.currentFamilyMember = null;
        this.currentMarket = null;
        this.currentBillContent = null;
        this.bill = new ArrayList<>();
    }

}
